package com.newenergy.wisehome.gateway.manager;

import android.os.Handler;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.broadlink.sdk.BLLet;
import cn.com.broadlink.sdk.data.controller.BLDNADevice;

public class BLLocalDeviceManager {

    public static final String TAG = "QBH";

    public static BLLocalDeviceManager localDeviceManager;
    private HashMap<String, BLDNADevice> mMapDeviceInSDK = new HashMap<>();
    private HashMap<String, BLDNADevice> mMapDevice = new HashMap<>();
    private ArrayList<BLDNADevice> devicesProbeInLan = new ArrayList<>();

    private ArrayList<BLDNADevice> devicesAddInSDK = new ArrayList<>();
//    private BLLocalDeviceListener blLocalDeviceListener;

    private Handler checkHandler = new Handler();
    private Runnable checkRunnable;

    public static BLLocalDeviceManager getInstance() {
        synchronized (BLLocalDeviceManager.class) {
            if (localDeviceManager == null) {
                localDeviceManager = new BLLocalDeviceManager();
            }
        }
        return localDeviceManager;
    }

    private BLLocalDeviceManager() {
//        BLLet.Controller.startProbe(3000);
//        BLLet.Controller.setOnDeviceScanListener(new BLDeviceScanListener() {
//            @Override
//            public boolean shouldAdd(BLDNADevice bldnaDevice) {
//                return false;
//            }
//
//            @Override
//            public void onDeviceUpdate(BLDNADevice bldnaDevice, boolean b) {
//                addDeviceInList(bldnaDevice);
//            }
//        });
    }

    private void addDeviceInList(BLDNADevice bldnaDevice) {
        if (!mMapDevice.containsKey(bldnaDevice.getDid())) {
            synchronized (mMapDevice) {
                mMapDevice.put(bldnaDevice.getDid(), bldnaDevice);
            }
//            if (blLocalDeviceListener != null) {
//                blLocalDeviceListener.deviceChange();
//            }
            Log.d(TAG, "add device :" + bldnaDevice.toString());
        } else {
            synchronized (mMapDevice) {
                mMapDevice.put(bldnaDevice.getDid(), bldnaDevice);
            }
        }
    }


    //添加设备到SDK
    public void addDeviceIntoSDK(BLDNADevice device) {
        BLLet.Controller.addDevice(device);
        synchronized (mMapDeviceInSDK) {
            mMapDeviceInSDK.put(device.getDid(), device);
        }
    }

    public ArrayList<BLDNADevice> getLocalDevices() {
        devicesProbeInLan.clear();
        // 获取所有的设备
        synchronized (mMapDevice) {
            for (Map.Entry<String, BLDNADevice> deviceEntry : mMapDevice.entrySet()) {
                devicesProbeInLan.add(deviceEntry.getValue());
            }
        }
        return devicesProbeInLan;
    }

    public List<BLDNADevice> getDevicesAddInSDK() {
        devicesAddInSDK.clear();
        synchronized (mMapDeviceInSDK) {
            for (Map.Entry<String, BLDNADevice> deviceEntry : mMapDeviceInSDK.entrySet()) {
                devicesAddInSDK.add(deviceEntry.getValue());
            }
        }
        return devicesAddInSDK;
    }

//    public void setBlLocalDeviceListener(BLLocalDeviceListener listener) {
//        blLocalDeviceListener = listener;
//    }
}
